﻿/*
LICENCE___________________________________________________________________
    Copyright (C) 2012 Reverdy Sylvain

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License along
    with this program; if not, write to the Free Software Foundation, Inc.,
    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
FIN LICENCE______________________________________________________________
*/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.IO;
using Merise_designer.Methodes;
using System.Xml.Xsl;
using System.Xml.XPath;
using System.Xml;
using Microsoft.Win32;
using System.Windows.Forms;

namespace Merise_designer
{
    public partial class Export : Form
    {
        Methodes.ObjetsCommun controller;
        List<ExportTools.myXslyFile> xsltFiles = new List<ExportTools.myXslyFile>();

        public Export(Methodes.ObjetsCommun controller)
        {
            InitializeComponent();
            this.StartPosition = FormStartPosition.CenterScreen;
            this.controller = controller;
        }

        private void Export_Load(object sender, EventArgs e)
        {

            foreach (String s in Directory.GetFiles(ExportTools.Export.getExportPath()))
            {
                if (Path.GetExtension(s) == ".xslt")
                {
                    xsltFiles.Add(new ExportTools.myXslyFile(s));
                }
            }

            refresh();
        }

        private void bt_Close_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        private void bt_copy_Click(object sender, EventArgs e)
        {
            System.Windows.Clipboard.SetData(DataFormats.Text, rt_script.Text);
            //ClipBoard.SetData(DataFormats.Rtf, richTextBox1.Rtf);
            MessageBox.Show("Script copié dans le presse papier.");
        }
        private void bt_Save_As_Click(object sender, EventArgs e)
        {
            Microsoft.Win32.SaveFileDialog saveFileDialog1 = new Microsoft.Win32.SaveFileDialog();
            saveFileDialog1.Filter = "SQL|*.sql";
            saveFileDialog1.Title = "Enregistrer un script sql sous *.sql";
            saveFileDialog1.ShowDialog();

            if (saveFileDialog1.FileName != "")
            {
                string path2 = saveFileDialog1.FileName;
                try
                {
                    // Ensure that the target does not exist.
                    rt_script.SaveFile(path2, RichTextBoxStreamType.PlainText);
                }

                catch
                {
                    Console.WriteLine("Erreur, impossible de créer le fichier.");
                }
                finally
                {
                }
            }

        }
        private void bt_export_xml_Click(object sender, EventArgs e)
        {
            Microsoft.Win32.SaveFileDialog saveFileDialog1 = new Microsoft.Win32.SaveFileDialog();
            saveFileDialog1.Filter = "XML|*.xml";
            saveFileDialog1.Title = "Enregistrer un fichier xml sous *.xml";
            saveFileDialog1.ShowDialog();

            if (saveFileDialog1.FileName != "")
            {
                string path2 = saveFileDialog1.FileName;
                try
                {
                    // Ensure that the target does not exist.
                    ExportTools.Export export = new ExportTools.Export();
                    export.exportToXml(this.controller, path2);
                }

                catch
                {
                    Console.WriteLine("Erreur, impossible de créer le fichier.");
                }
                finally
                {
                }
            }

        }

        private void bt_generate_Click(object sender, EventArgs e)
        {

            //MessageBox.Show(cmb_choice.SelectedValue.ToString());
            
            rt_script.Clear();
            ExportTools.Export export = new ExportTools.Export();
            XmlTextWriter myWriter = null;
            String xmlFile="";
            String xmlFileOut="";
            String xsltFile = cmb_choice.SelectedValue.ToString();
            if (xsltFile == null || xsltFile == "" || !File.Exists(xsltFile)) return;
            try
            {

                //Créé le fichier xml
                String alea = export.RandomString(5, false);
                 xmlFile = Openroject.getActualProjectFIle().Replace(Configuration.extensionFile, "") + alea + ".xml";
                export.exportToXml(this.controller, xmlFile);

                XPathDocument myXPathDoc = new XPathDocument(xmlFile);
                
                //Charge le fichier xslt
                XslCompiledTransform myXslTrans = new XslCompiledTransform();
                myXslTrans.Load(xsltFile);

                //XML de sortie
                xmlFileOut = Openroject.getActualProjectFIle().Replace(Configuration.extensionFile, "") + alea + ".txt";
                myWriter = new XmlTextWriter(xmlFileOut, null);

                //Opère la transformation
                myXslTrans.Transform(myXPathDoc, null, myWriter);
                myWriter.Flush();
                myWriter.Close();

                //Ouvre le fichier dans le textBox
                rt_script.LoadFile(xmlFileOut, RichTextBoxStreamType.PlainText);
            }
            catch
            {
                MessageBox.Show("Une erreure est survenue, opération annulée", "Erreur", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            finally
            {
                if (myWriter != null)
                {
                    try
                    {
                        myWriter.Close();
                    }
                    catch { }
                }
                if(File.Exists(xmlFile)) File.Delete(xmlFile);
                if (File.Exists(xmlFileOut)) File.Delete(xmlFileOut);
            }
        }

        private void bt_browse_Click(object sender, EventArgs e)
        {
            Microsoft.Win32.OpenFileDialog browseFile = new Microsoft.Win32.OpenFileDialog();
           // browseFile.Filter = "Merise designer Files (*" + Configuration.extensionFile + ")|*" + Configuration.extensionFile + "";

            browseFile.Filter = "XSLT Files (*.xslt)|*.xslt";
            browseFile.Title = "Choisir un fichier xslt";
            browseFile.Multiselect = false;
            if (browseFile.ShowDialog()==false) return;

            xsltFiles.Add(new ExportTools.myXslyFile(browseFile.FileName));
            refresh();
        }

        private void refresh()
        {
            rt_script.Clear();

            cmb_choice.DataSource = new List<ExportTools.myXslyFile>();
            cmb_choice.DataSource = xsltFiles;
            cmb_choice.DisplayMember = "name";
            cmb_choice.ValueMember = "path";
        }
    }
}
